﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using DevComponents.DotNetBar;  
using System.Collections;       
using UFIDA.U8.DB;

namespace UFIDA.U8C.AnLian.NBBankSync
{
    public partial class BaseSetUI : Form
    {
        private string _connectstring = Common.getConfigInfo("ConnectString");
        private BankAccUI frmBankAccUI;
        public BaseSetUI()
        {
            // TODO: Complete member initialization
            InitializeComponent();       
            this.initData(); 
        }       

        private void initData()
        {
            StringBuilder sqlAccount = new StringBuilder();
            sqlAccount.Append("select * from cust_syncacount ");
          
            try
            {
                ds_account = DbHelperSQL.Query(sqlAccount.ToString(), "cust_syncacount", _connectstring); 
                
                bdsaccount.DataSource = ds_account;
                bdsaccount.DataMember = "cust_syncacount"; 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            } 
        }
         

        private void toolStripButton1_Click(object sender, EventArgs e)
        { 
            bdsaccount.EndEdit();

            try
            {
                SqlDataAdapter dal = new SqlDataAdapter("select * from cust_syncacount where 1=2", _connectstring);

                SqlCommandBuilder scb = new SqlCommandBuilder(dal);
                dal.Update(ds_account, "cust_syncacount");
                ds_account.AcceptChanges();
                ////判断是否是新添加的账套，若是新添加的账套，需要添加时间戳配置
                //for (int i = 0; i < ds_account.Tables[0].DefaultView.Count; i++)
                //{
                //    string sql = "select * from SyncConfig where accid='" + ds_account.Tables[0].DefaultView[i]["accname"].ToString()+ "'";
                //    DataTable ds = DbHelperSQL.QueryAsDT(sql, _connectstring);
                //    if (ds == null || ds.DefaultView.Count == 0)
                //    {
                //        string sql2 = "insert into SyncConfig(accid,record) values ('" + ds_account.Tables[0].DefaultView[i]["accname"] + "','inventory')";
                //        DbHelperSQL.ExecuteSql(sql2, _connectstring);
                //        sql2 = "insert into SyncConfig(accid,record) values ('" + ds_account.Tables[0].DefaultView[i]["accname"] + "','customer')";
                //        DbHelperSQL.ExecuteSql(sql2, _connectstring);
                //        sql2 = "insert into SyncConfig(accid,record) values ('" + ds_account.Tables[0].DefaultView[i]["accname"] + "','vendor')";
                //        DbHelperSQL.ExecuteSql(sql2, _connectstring);
                //    }
                //}
                MessageBox.Show("保存成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dgvaccount_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        { 
            if (this.dgvaccount.Columns[e.ColumnIndex].Name == "psd")
            {
                if (e.Value != null && e.Value.ToString().Length > 0)
                {
                    e.Value = new string('*', 6);
                }
            }

        }

        private void BaseSetUI_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgvaccount.CurrentRow == null)
                {
                    MessageBox.Show("请选择账套");
                    return;
                }
                if (frmBankAccUI == null)
                {
                    frmBankAccUI = new BankAccUI();
                }
                frmBankAccUI._Accid = dgvaccount.CurrentRow.Cells["accname"].Value.ToString();
                frmBankAccUI.ShowDialog();
            }
            finally
            {
                frmBankAccUI = null;
            }
            
        }  
    }
}
